package junit;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.DefaultTransactionDefinition;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({ "classpath:applicationContext.xml" })
public class BaseJunit {

	@Autowired
	private PlatformTransactionManager transactionManager;

	private TransactionStatus transaction;

	@Test
	public void test() {
		// do nothing
	}

	protected void transactionBegin() {
		transaction = transactionManager.getTransaction(new DefaultTransactionDefinition());
	}

	protected void transactionRollback() {
		org.springframework.util.Assert.notNull(transaction, "请在@Begin中调用transactionBegin方法");
		transactionManager.rollback(transaction);
	}
}
